package test;

import javax.enterprise.deploy.shared.StateType;
import javax.enterprise.deploy.shared.factories.DeploymentFactoryManager;
import javax.enterprise.deploy.spi.DeploymentManager;
import javax.enterprise.deploy.spi.exceptions.DeploymentManagerCreationException;
import javax.enterprise.deploy.spi.status.DeploymentStatus;

//import org.jboss.as.controller.client.helpers.standalone.impl.DeploymentActionImpl;
//import org.jboss.deployment.spi.DeploymentManagerImpl;
//import org.jboss.deployment.spi.factories.DeploymentFactoryImpl;
//import org.jboss.as.ee.deployment.spi.factories.DeploymentFactoryImpl;

import org.jboss.as.ee.deployment.spi.DeploymentManagerImpl;
import org.jboss.as.ee.deployment.spi.factories.DeploymentFactoryImpl;

/**Utilities for the JSR88 JBoss deployer.
 * 
 * @author Wolfgang Knauf
 * 
 * @version 2011-01-12  First version (created from AS4 deployer file)
 */
public class JSR88DeployerUtil
{

  /**Get the deployment manager of a specific server on a specific port.
   * 
   * @param _strServer Server to connect, e.g. "localhost"
   * @param _strPort JNDI Port, default is 1099 
   * @return An instance of the deployment manager, or NULL if an error occured (see output in this case)
   */
  public static DeploymentManager getDeploymentManager(String _strServer, String _strPort)
  {
    //JBoss 6 and before...
    //System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
    //System.setProperty(Context.URL_PKG_PREFIXES, "org.jboss.naming.client");
    //Build server URL:
    //String strUrl = "jnp://" + _strServer + ":" + _strPort;
    //System.setProperty(Context.PROVIDER_URL, "jnp://localhost:1099");
    //System.setProperty(Context.PROVIDER_URL, strUrl);

    //JBOSS 5 requires a call to method "DeploymentFactoryImpl.register", otherwise no DeploymentFactories are found.
    //JBoss 4.2 does not know this method. 
    //So I use reflection to find this method. If it is present, then it is called.
    //This code will not compile against JBoss 4.2, but it will at least run, because
    //this branch is not entered ;-).
    /*
    boolean bolContainsRegister = false;
    java.lang.reflect.Method[] methods = DeploymentFactoryImpl.class.getMethods();
    for (int intMethod = 0; intMethod < methods.length && bolContainsRegister == false; intMethod++)
    {
      if ("register".equals(methods[intMethod].getName() ) == true)
      {
        bolContainsRegister = true;
        DeploymentActionImpl.register();
      }
    }*/
    //Register for AS7!
    boolean bolChangeAS7_1;
    DeploymentFactoryImpl.register();
    
    DeploymentFactoryManager dfm = DeploymentFactoryManager.getInstance();

    //Debugging: output deployment factories...
    /*
    DeploymentFactory[] factories = dfm.getDeploymentFactories();
    System.out.println(factories.length);
    for (int i = 0; i < factories.length; i++)
    {
      System.out.println("Factory: " + factories[i]);
    }*/
    
    
    DeploymentManager dm = null;
    try
    {
      //Modified URL for AS7!
      boolean bolChangeAS7_2;
      //dm = dfm.getDeploymentManager(DeploymentManagerImpl.DEPLOYER_URI, null, null);
      String sURL = DeploymentManagerImpl.DEPLOYER_URI + "?targetType=as7&serverHost=" + _strServer + "&serverPort=" + _strPort;
      System.out.println ("getDeploymentManager for URL " + sURL);
      dm = dfm.getDeploymentManager(sURL, null, null);
    }
    catch (DeploymentManagerCreationException e)
    {
      System.err.println ("Could not get DeploymentManager: " + e.toString());
    }
    
    return dm;
  }
  
  /**Wait for completion of a DeploymentStatus (wait as long as the "StateType" is "RUNNING")
   * 
   * @param status This is the DeploymentStatus on whose completion we wait.
   * @throws InterruptedException If waiting for completion is interrupted (not expected here)
   */
  public static void waitForCompletion(DeploymentStatus status) throws InterruptedException
  {
    // wait for the deployment to finish
    while (StateType.RUNNING == status.getState())
    {
      Thread.sleep(100);
    }
  }

}
